package cn.whdreamblog.entites;

import java.util.List;

public class PageBean {
	private List list;// 要返回的某一页的记录列表
	
	private long allRow;//总记录数
	private long totalPage;//总页数
	private int currentPage;//当前页码
	private boolean isFirstPage = false;//是否是第一页
	private boolean isLastPage = false;//是否是最后一页
	private boolean hasPreviousPage = false;//是否有前一页
	private boolean hasNextPage = false;//是否有后一页
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public long getAllRow() {
		return allRow;
	}
	public void setAllRow(long allRow) {
		this.allRow = allRow;
	}
	public long getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public boolean isFirstPage() {
		return isFirstPage;
	}
	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}
	public boolean isLastPage() {
		return isLastPage;
	}
	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}
	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}
	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}
	public boolean isHasNextPage() {
		return hasNextPage;
	}
	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}
	
	 /**
	   * 计算总页数,静态方法,供外部直接通过类名调用
	   * 
	   * @param pageSize每页记录数
	   * @param allRow总记录数
	   * @return 总页数
      */
	   public static long countTotalPage(final int pageSize, final long allRow) {
	        long totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow / pageSize + 1;
	        return totalPage;
	    }
	   
	   /**
	       * 计算当前页开始记录
	        * 
	        * @param pageSize每页记录数
	        * @param currentPage当前第几页
	       * @return 当前页开始记录号
	        */
	       public static int countOffset(final int pageSize, final int currentPage) {
	           final int offset = pageSize * (currentPage - 1);
	           return offset;
	       }
	   
	      /**
	       * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
	       * 
	        * @paramPage 传入的参数(可能为空,即0,则返回1)
	       * @return 当前页
	        */
	       public static int countCurrentPage(int page) {
	           final int curPage = (page == 0 ? 1 : page);
	         return curPage;
	      }
	   
	   
	   public static PageBean  setPageData(List list,PageBean page,long allRow,int limit,int p ) {	
		   page.setAllRow(allRow);
		   page.setList(list);
		   page.setTotalPage(PageBean.countTotalPage(limit, allRow));
		   page.setCurrentPage(countCurrentPage(p));
		   page.setHasNextPage(true);
		   page.setHasPreviousPage(true);
		   if(1 == page.getCurrentPage()){
			   page.setFirstPage(true);
			   page.setHasNextPage(true);
			   page.setHasPreviousPage(false);
		   }
		   if(page.getCurrentPage() >= page.getTotalPage() ){
			   page.setHasNextPage(false);
			   if(page.getCurrentPage()!= 1){
				   page.setHasPreviousPage(true);
			   }
			   page.setLastPage(true);
		   }
		   return page;
	   }
}
